home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj1191.zip / DEVLOD.ZIP / C0.ASM next >
Assembly Source File  |  1990-09-25  |  6KB  |  223 lines

  1.         NAME    c0
  2. ;[]------------------------------------------------------------[]
  3. ;|      C0.ASM -- Start Up Code                                 |
  4. ;|        based on Turbo-C startup code, extensively modified   |
  5. ;[]------------------------------------------------------------[]
  6.  
  7. _TEXT   SEGMENT BYTE PUBLIC 'CODE'
  8. _TEXT   ENDS
  9.  
  10. _DATA   SEGMENT WORD PUBLIC 'DATA'
  11. _DATA   ENDS
  12.  
  13. _BSS    SEGMENT WORD PUBLIC 'BSS'
  14. _BSS    ENDS
  15.  
  16. DGROUP  GROUP   _TEXT, _DATA, _BSS
  17.  
  18. ;       External References
  19.  
  20. EXTRN   _main : NEAR
  21. EXTRN   _exit : NEAR
  22.  
  23. EXTRN   __stklen : WORD
  24. EXTRN   __heaplen : WORD
  25.  
  26. PSPHigh         equ     00002h
  27. PSPEnv          equ     0002ch
  28.  
  29. MINSTACK        equ     128     ; minimal stack size in words
  30.  
  31. ;       At the start, DS, ES, and SS are all equal to CS
  32.  
  33. ;/*-----------------------------------------------------*/
  34. ;/*     Start Up Code                                   */
  35. ;/*-----------------------------------------------------*/
  36.  
  37. _TEXT   SEGMENT BYTE PUBLIC 'CODE'
  38.  
  39. ASSUME  CS:_TEXT, DS:DGROUP
  40.  
  41.         ORG     100h
  42.  
  43. STARTX  PROC    NEAR
  44.  
  45.         mov     dx, cs          ; DX = GROUP Segment address
  46.         mov     DGROUP@, dx
  47.         mov     ah, 30h         ; get DOS version
  48.         int     21h
  49.         mov     bp, ds:[PSPHigh]; BP = Highest Memory Segment Addr
  50.         mov     word ptr __heaptop, bp
  51.         mov     bx, ds:[PSPEnv] ; BX = Environment Segment address
  52.         mov     __version, ax   ; Keep major and minor version number
  53.         mov     __psp, es       ; Keep Program Segment Prefix address
  54.  
  55. ;       Determine the amount of memory that we need to keep
  56.  
  57.         mov     dx, ds          ; DX = GROUP Segment address
  58.         sub     bp, dx          ; BP = remaining size in paragraphs
  59.         mov     di, __stklen    ; DI = Requested stack size
  60. ;
  61. ; Make sure that the requested stack size is at least MINSTACK words.
  62. ;
  63.         cmp     di, 2*MINSTACK  ; requested stack big enough ?
  64.         jae     AskedStackOK    ; yes, use it
  65.         mov     di, 2*MINSTACK  ; no,  use minimal value
  66.         mov     __stklen, di    ; override requested stack size
  67. AskedStackOK:
  68.         add     di, offset DGROUP: edata
  69.         jb      InitFailed      ; DATA segment can NOT be > 64 Kbytes
  70.         add     di, __heaplen
  71.         jb      InitFailed      ; DATA segment can NOT be > 64 Kbytes
  72.         mov     cl, 4
  73.         shr     di, cl          ; $$$ Do not destroy CL $$$
  74.         inc     di              ; DI = DS size in paragraphs
  75.         cmp     bp, di
  76.         jnb     TooMuchRAM      ; Enough to run the program
  77.  
  78. ;       All initialization errors arrive here
  79.  
  80. InitFailed:
  81.         jmp     near ptr _abort
  82.  
  83. ;       Set heap base and pointer
  84.  
  85. TooMuchRAM:
  86.         mov     bx, di          ; BX = total paragraphs in DGROUP
  87.         shl     di, cl          ; $$$ CX is still equal to 4 $$$
  88.         add     bx, dx          ; BX = seg adr past DGROUP
  89.         mov     __heapbase, bx
  90.         mov     __brklvl, bx
  91. ;
  92. ;       Set the program stack down into RAM that will be kept.
  93. ;
  94.         cli
  95.         mov     ss, dx          ; DGROUP
  96.         mov     sp, di          ; top of (reduced) program area
  97.         sti
  98.  
  99.         mov     bx,__heaplen    ; set up heap top pointer
  100.         add     bx,15
  101.         shr     bx,cl           ; length in paragraphs
  102.         add     bx,__heapbase
  103.         mov     __heaptop, bx
  104. ;
  105. ;       Clear uninitialized data area to zeroes
  106. ;
  107.         xor     ax, ax
  108.         mov     es, cs:DGROUP@
  109.         mov     di, offset DGROUP: bdata
  110.         mov     cx, offset DGROUP: edata
  111.         sub     cx, di
  112.         rep     stosb
  113. ;
  114. ;       exit(main());
  115. ;
  116.         call    _main           ; the real C program
  117.         push    ax
  118.         call    _exit           ; part of the C program too
  119.  
  120. ;----------------------------------------------------------------
  121. ;       _exit()
  122. ;       Restore interrupt vector taken during startup.
  123. ;       Exit to DOS.
  124. ;----------------------------------------------------------------
  125.  
  126.         PUBLIC  __exit
  127. __exit  PROC      NEAR
  128.         push    ss
  129.         pop     ds
  130.  
  131. ;       Exit to DOS
  132.  
  133. ExitToDOS:
  134.         mov     bp,sp
  135.         mov     ah,4Ch
  136.         mov     al,[bp+2]
  137.         int     21h                     ; Exit to DOS
  138.  
  139. __exit  ENDP
  140.  
  141. STARTX  ENDP
  142.  
  143. ;[]------------------------------------------------------------[]
  144. ;|      Miscellaneous functions                                 |
  145. ;[]------------------------------------------------------------[]
  146.  
  147. ErrorDisplay    PROC    NEAR
  148.                 mov     ah, 040h
  149.                 mov     bx, 2           ; stderr device
  150.                 int     021h
  151.                 ret
  152. ErrorDisplay    ENDP
  153.  
  154.         PUBLIC  _abort
  155. _abort  PROC      NEAR
  156.                 mov     cx, lgth_abortMSG
  157.                 mov     dx, offset DGROUP: abortMSG
  158. MsgExit3        label   near
  159.                 push    ss
  160.                 pop     ds
  161.                 call    ErrorDisplay
  162. CallExit3       label   near
  163.                 mov     ax, 3
  164.                 push    ax
  165.                 call    __exit          ; _exit(3);
  166. _abort   ENDP
  167.  
  168. ;       The DGROUP@ variable is used to reload DS with DGROUP
  169.  
  170.         PUBLIC  DGROUP@
  171. DGROUP@     dw    ?
  172.  
  173. _TEXT   ENDS
  174.  
  175. ;[]------------------------------------------------------------[]
  176. ;|      Start Up Data Area                                      |
  177. ;[]------------------------------------------------------------[]
  178.  
  179. _DATA   SEGMENT WORD PUBLIC 'DATA'
  180.  
  181. abortMSG        db      'Quitting program...', 13, 10
  182. lgth_abortMSG   equ     $ - abortMSG
  183.  
  184. ;
  185. ;                       Miscellaneous variables
  186. ;
  187.         PUBLIC  __psp
  188.         PUBLIC  __version
  189.         PUBLIC  __osmajor
  190.         PUBLIC  __osminor
  191.  
  192. __psp           dw      0
  193. __version       label   word
  194. __osmajor       db      0
  195. __osminor       db      0
  196.  
  197. ;       Memory management variables
  198.  
  199.         PUBLIC  ___heapbase
  200.         PUBLIC  ___brklvl
  201.         PUBLIC  ___heaptop
  202.         PUBLIC  __heapbase
  203.         PUBLIC  __brklvl
  204.         PUBLIC  __heaptop
  205.  
  206. ___heapbase     dw      DGROUP:edata
  207. ___brklvl       dw      DGROUP:edata
  208. ___heaptop      dw      DGROUP:edata
  209. __heapbase      dw      0
  210. __brklvl        dw      0
  211. __heaptop       dw      0
  212.  
  213. _DATA   ENDS
  214.  
  215. _BSS    SEGMENT WORD PUBLIC 'BSS'
  216.  
  217. bdata   label   byte
  218. edata   label   byte            ; mark top of used area
  219.  
  220. _BSS    ENDS
  221.  
  222.         END     STARTX
  223.